package com.ycode.bang.anbang.utils;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtil {

	public String sdPathStr; // 返回程序存储的目录
	public String sdRootPath; // 返回SD卡目录

	/**
	 * 初始化FileUtil,传入程序名,创建程序根目录
	 * */
	public FileUtil(Context context, String name) {
		File file = null;
		boolean isSDExit = checkSD();
		if (isSDExit) {
			file = Environment.getExternalStorageDirectory();// 获取跟目录
		} else {
			file = context.getApplicationContext().getFilesDir();
		}
		sdRootPath = file.toString();
		sdPathStr = sdRootPath + "/" + name;
		File dingDongFile = new File(sdPathStr);
		if (!dingDongFile.exists()) {
			dingDongFile.mkdir();
		}
		dingDongFile.exists();
	}

	/**
	 * 通过字符数组把图片写入SD卡程序根目录下
	 * 
	 * @throws IOException
	 * */
	public void pictureWriteIntoSD(String name, byte[] buffer)
			throws IOException {
		File file = new File(getFileAbsolutePath(name));
		BufferedOutputStream outputStream = new BufferedOutputStream(
				new FileOutputStream(file));
		outputStream.write(buffer);
		outputStream.flush();
		outputStream.close();
	}

	/**
	 * 在程序根目录下判断一个文件是否存在
	 * */
	public boolean judgeFileExist(String name) {
		File file = new File(getFileAbsolutePath(name));
		if (file.exists()) {
			if (file.isFile()) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	/**
	 * 得到文件全路径
	 * */
	public String getFileAbsolutePath(String name) {

		String filePath = sdPathStr + "/" + name;

		return filePath;
	}
	/**
	 * 通过名称得到File对象
	 * */
	public File getObjectPathFile(String name) {
		File file = new File(getFileAbsolutePath(name));
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return file;
	}
	/**
	 * 检查SD卡是否存在
	 * 
	 */
	private boolean checkSD() {
		boolean isSDExit = false;
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			isSDExit = true;
		} else {
			isSDExit = false;
		}
		return isSDExit;
	}

	public boolean mvFile(String name, String objectPath, String objectName) {
		try {
			File file = new File(objectPath);
			if (!file.exists()) {
				file.mkdirs();
			}
			BufferedInputStream inputStream = new BufferedInputStream(
					new FileInputStream(new File(getFileAbsolutePath(name))));
			BufferedOutputStream outputStream = new BufferedOutputStream(
					new FileOutputStream(
							new File(objectPath + "/" + objectName)));
			byte[] buffer = new byte[2048];
			int temp = -1;
			try {
				while ((temp = inputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, temp);
					outputStream.flush();
				}

				inputStream.close();
				outputStream.close();
				return true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 返回此目录大小,以kb为单位
	 * 
	 * @return
	 */
	public long getSize() {
		File file = new File(sdPathStr);
		long size = 0;
		File[] files = file.listFiles();
		for (File file2 : files) {
			size += file2.length();
		}
		return size;
	}

	/**
	 * 清理此目录里面所有文件
	 * 
	 * @return
	 */
	public void clear() {
		deleteFilesByDirectory(new File(sdPathStr));
//		File file = new File(sdPathStr);
//		System.out.println("sdPathStr--" + sdPathStr);
//		file.delete();
//		file.mkdir();
	}

	private static void deleteFilesByDirectory(File directory) {
		if (directory != null && directory.exists() && directory.isDirectory()) {
			for (File item : directory.listFiles()) {
				item.delete();
			}
		}
	}
}
